//
//  AppListTableViewCell.m
//  LimitFree
//
//  Created by lijinghua on 16/3/1.
//  Copyright © 2016年 lijinghua. All rights reserved.
//

#import "AppListTableViewCell.h"
#import "LimitFreeUtility.h"

@interface AppListTableViewCell ()
{
    AppListModel *_model;
    UIView       *_deleteLineView;
}

@end

@implementation AppListTableViewCell

//当cell通过xib构建好之后调用函数
- (void)awakeFromNib {
    self.iconImageView.layer.cornerRadius = 8;
    self.iconImageView.layer.masksToBounds = YES;
    
    //设置选中时显示的背景
    self.selectedBackgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"appproduct_loadingviewcell_ligh_2t@2x.png"]];
    //创建删除线
    _deleteLineView = [LimitFreeUIFactory createLineView:[UIColor blackColor] frame:CGRectMake(0, 9, 50, 1)];
    
    [self.priceLabel addSubview:_deleteLineView];
}


- (void)updateWithModel:(AppListModel*)model atIndexPath:(NSIndexPath*)indexPath type:(NSString*)type{
    _model = model;
    //根据行号决定背景
    [self changeCellBackgroundView:indexPath];
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.iconUrl]];
    self.nameLabel.text = model.name;
    self.otherInfoLabel.text = [self getOtherInformation:type];
    self.countInfoLabel.text = [NSString stringWithFormat:@"分享:%@ 收藏:%@ 下载:%@",model.shares,model.favorites,model.downloads];
    self.priceLabel.text = model.lastPrice;
    self.categoryLabel.text = model.categoryName;
    
    //设置星级
    [self.levelView setStarLevel:model.starCurrent];
}

- (NSString*)getOtherInformation:(NSString*)type{
    if ([type isEqualToString:kLimitType]) {
        //计算当前的时间和过期时间的差值
        //以格式化的形式显示出来
        NSString *leftTime = [LimitFreeUtility calculateDiffTimeToDate:_model.expireDatetime];
        return [NSString stringWithFormat:@"剩余:%@",leftTime];
    }else if([type isEqualToString:kReduceType]){
        return [NSString stringWithFormat:@"现价:%@",_model.currentPrice];
    }else{
        return [NSString stringWithFormat:@"评分:%@",_model.starCurrent];
    }
}

- (void)changeCellBackgroundView:(NSIndexPath*)indexPath{
    if (indexPath.row % 2 == 0) {
        UIImage *image = [UIImage imageNamed:@"cate_list_bg1.png"];
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(20, 200, 20, 100)];
        
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = image;
        self.backgroundView = imageView;
    }else{
        UIImage *image =  [UIImage imageNamed:@"cate_list_bg2.png"];
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(20, 200, 20, 100)];
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = image;
        self.backgroundView = imageView;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    _deleteLineView.backgroundColor = [UIColor blackColor];
}

@end
